package com.heima.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.Result;
import com.heima.domain.dto.LoginDto;
import com.heima.domain.dto.UserDto;
import com.heima.domain.po.Role;
import com.heima.domain.po.User;
import com.heima.domain.vo.LoginUserVo;
import com.heima.domain.vo.RoleVo;
import com.heima.domain.vo.UserPageVo;
import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
        }


        User user = userMapper.selectOne(dto);
        //代表用户不存在得
        if(user == null){

        }
        //代表密码错误
        if(!user.getPassword().equals(dto.getPassword())){

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    /**
     * 人员分页查询
     *
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @Override
    public Result getByPage(Integer pageIndex, Integer pageSize, String userName) {
        Page<User> page = new Page<>(pageIndex,pageSize);
        Page<User> page1 = lambdaQuery().like(StringUtils.hasText(userName), User::getUserName, userName).page(page);
        List<UserPageVo> userPageVos = BeanUtil.copyToList(page1.getRecords(), UserPageVo.class);
        userPageVos.stream().forEach(vo->{
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(vo.getRoleId() != null,Role::getRoleId,vo.getRoleId());
            Role role = roleMapper.selectOne(wrapper);
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(role, roleVo);
            vo.setRole(roleVo);
            vo.setPassword("");
            vo.setSecret("");
        });
        Result of = Result.of(page1);
        of.setCurrentPageRecords(userPageVos);
        return of;
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<RoleVo> getAllRole() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        List<Role> roles = roleMapper.selectList(wrapper);
        List<RoleVo> list = BeanUtil.copyToList(roles, RoleVo.class);
        return list;
    }

    /**
     * 添加人员
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean addUser(UserDto dto) {
        User user = new User();
        BeanUtils.copyProperties(dto,user);
        try {
            this.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 修改人员
     *
     * @param dto
     * @param userId
     * @return
     */
    @Override
    public Boolean updateUser(UserDto dto, Integer userId) {
        User user = new User();
        BeanUtils.copyProperties(dto,user);
        try {
            lambdaUpdate().eq(userId != null,User::getId,userId).update(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
